package com.io.example.demoparent.service.impl;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * 该类是根据优惠券类型resourceType、编码resourceId 去查询 发放方式grantType的实现类
 * @program: demo-project
 * @ClassName QueryGrantTypeService
 * @description:
 * @author: 宠儿
 * @create: 2024−09-01 15:01
 * @Version 1.0
 **/

@Service("queryGrantTypeService")
public class QueryGrantTypeService {

    @Resource
    private GrantTypeServiceImpl grantTypeService;

    private Map<String, Function<String, String>> grantTypeMap = new HashMap<>();

    @PostConstruct
    public void dispatcherInit(){
        grantTypeMap.put("红包", s -> grantTypeService.redPaper(s));
        grantTypeMap.put("购物券", s -> grantTypeService.shopping(s));
        grantTypeMap.put("qq会员", s -> grantTypeService.QQVip(s));
    }


    /**
     * 根据优惠券类型resourceType、编码resourceId 去查询 发放方式grantType
     * @param resourceType
     * @param resourceId
     * @return
     */
    public String getResult(String resourceType, String resourceId) {
        //Controller根据 优惠券类型resourceType、编码resourceId 去查询 发放方式grantType
        Function<String, String> result = grantTypeMap.get(resourceType);
        if (result != null) {
            //传入resourceId 执行这段表达式获得String型的grantType
            return result.apply(resourceId);
        }
        return "查询不到该优惠券的发放方式";
    }
}

